package rexsee.core.storage;

import java.util.HashMap;
import java.util.Iterator;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.utilities.Json;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RexseeCursor implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Cursor";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeCursor(childBrowser);
	}

	protected final Browser mBrowser;

	protected final HashMap<String, SQLiteDatabase> dbs = new HashMap<String, SQLiteDatabase>();
	protected final HashMap<String, Cursor> cursors = new HashMap<String, Cursor>();

	public RexseeCursor(Browser browser) {
		mBrowser = browser;
	}

	//JavaScript Interface

	public boolean openDB(String id, String dbPath) {
		if (dbs.containsKey(id)) closeDB(id);
		try {
			SQLiteDatabase db = RexseeDatabase.getDatabase(mBrowser, dbPath);
			if (db == null) return false;
			dbs.put(id, db);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public void closeDB(String id) {
		if (!dbs.containsKey(id)) return;
		try {
			SQLiteDatabase db = dbs.get(id);
			if (db != null) {
				db.close();
			}
		} catch (Exception e) {
		}
		dbs.remove(id);
	}
	public boolean existsDB(String id) {
		return dbs.containsKey(id);
	}

	public boolean create(String cursorId, String dbId, String sql) {
		if (cursors.containsKey(cursorId)) close(cursorId);
		try {
			SQLiteDatabase db = dbs.get(dbId);
			if (db == null) return false;
			Cursor cursor = db.rawQuery(sql, null);
			if (cursor == null) return false;
			cursors.put(cursorId, cursor);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public void close(String cursorId) {
		if (!cursors.containsKey(cursorId)) return;
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor != null) {
				cursor.close();
			}
		} catch (Exception e) {
		}
		cursors.remove(cursorId);
	}
	public boolean exists(String cursorId) {
		return cursors.containsKey(cursorId);
	}

	public String readByColumnName(String cursorId, int line, String columnName) {
		return readByColumnIndex(cursorId, line, getColumnIndex(cursorId, columnName));
	}
	public String readByColumnIndex(String cursorId, int line, int columnIndex) {
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor == null) return null;
			if (line < 0 || line >= cursor.getCount()) return null;
			if (columnIndex < 0 || columnIndex >= cursor.getColumnCount()) return null;
			cursor.moveToPosition(line);
			return cursor.getString(columnIndex);
		} catch (Exception e) {
			return null;
		}
	}

	public int getColumnCount(String cursorId) {
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor == null) return -1;
			return cursor.getColumnCount();
		} catch (Exception e) {
			return -1;
		}
	}
	public int getCount(String cursorId) {
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor == null) return -1;
			return cursor.getCount();
		} catch (Exception e) {
			return -1;
		}
	}
	public int getColumnIndex(String cursorId, String columnName) {
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor == null) return -1;
			return cursor.getColumnIndex(columnName);
		} catch (Exception e) {
			return -1;
		}
	}
	public String getColumnName(String cursorId, int columnIndex) {
		try {
			Cursor cursor = cursors.get(cursorId);
			if (cursor == null) return null;
			return cursor.getColumnName(columnIndex);
		} catch (Exception e) {
			return null;
		}
	}

	public int countDB() {
		return dbs.size();
	}
	public String getDBs() {
		return Json.toJson(dbs.keySet().iterator());
	}
	public int countCursor() {
		return cursors.size();
	}
	public String getCursors() {
		return Json.toJson(cursors.keySet().iterator());
	}

	public void closeAll() {
		Iterator<String> i1 = cursors.keySet().iterator();
		while (i1.hasNext()) {
			try {
				Cursor cursor = cursors.get(i1.next());
				if (cursor != null) cursor.close();
			} catch (Exception e) {
			}
		}
		cursors.clear();
		Iterator<String> i2 = dbs.keySet().iterator();
		while (i2.hasNext()) {
			try {
				SQLiteDatabase db = dbs.get(i2.next());
				if (db != null) db.close();
			} catch (Exception e) {
			}
		}
		dbs.clear();
	}

}
